package org.mobile.servlets;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.mobile.beans.User;
import org.mobile.logic.UserLogic;

/**
 * Servlet implementation class ChangeTheme
 */
@WebServlet(name = "changeTheme", urlPatterns = { "/changeTheme" })
public class ChangeTheme extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ChangeTheme() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int userID= ((User)request.getSession().getAttribute("user")).getId();
		int theme= Integer.parseInt(request.getParameter("theme"));
		if(!UserLogic.getInstance().changeTheme(userID, theme)){
			request.setAttribute("error", "Błąd zmiany motywu!");
			request.getRequestDispatcher("errPage.jsp").forward(request, response);
		}
		else{
			((User)request.getSession().getAttribute("user")).setStyle(theme); // DKA: dopisa�em, bo zmienia�o si� w bazie, ale nie w obiekcie usera w sesji a to jest nam potrzebne :)
			request.setAttribute("message", "Motyw został zmieniony");
			request.getRequestDispatcher("index.jsp").forward(request, response);
			
		}
	}

}
